/*
 * GameMember.h
 *
 *  Created on: 2013.01.12.
 *      Author: Thg
 */

#ifndef GAMEMEMBER_H_
#define GAMEMEMBER_H_

#include "GraphicalObject.h"

#include <vector>

class GameMember;

struct CollisionInfo
{
	GameMember* mHitObject;
	float 		mHitTime;
	Vector		mHitPoint;
};

class CollisionInfoList
{
public:
	CollisionInfoList();
	CollisionInfoList(CollisionInfo collisionInfo);

	void operator+=(const CollisionInfoList& list); // Append new list.
	void operator+=(const CollisionInfo& collisionInfo); // Append a single CollisionInfo.

	const std::vector<CollisionInfo>& getList() const;
	void sortByHitTime();
	bool getFistHit(CollisionInfo& collisionInfo);

private:
	std::vector<CollisionInfo> mCollisions;
	bool mIsSorted;
};

class GameMember : public GraphicalObject
{
public:
	GameMember(Vector pos);
	~GameMember();

	void join(GameMember* member);
	void unjoin();

	void addChild(GameMember* child);
	void insertChild(GameMember* child, unsigned int index);
	void removeChild(unsigned int index, bool doDelete);
	void removeChild(GameMember* child);
	GameMember* getChild(unsigned int index);
	bool getChildIndex(GameMember* child, unsigned int& index);
	unsigned int countChildren() const;

	void interact(GameMember* obj);
	CollisionInfoList collide(GameMember* obj, GameMember* source, bool dontHitSource = true);
	void control(float dt);
	void animate(float dt);
	void beforeDraw();
	void draw(Camera* camera);
	void afterDraw();
	bool isAlive();
	static void buryDead(GameMember* exclude);

private:
	static GameMember*		 mRoot;

	std::vector<GameMember*> mChildren;
	GameMember* 			 mParent;
	GameMember*				 mNext;
	GameMember*				 mPrev;
	bool					 mIsAlive;
};

#endif /* GAMEMEMBER_H_ */
